function choseFunction(workarea,counter){
	if(workarea == "null")
		loadDataAndWorkArea();
	else
		loadDataAndCounter();
}

function upDateCounter(){
	waitingDialog({});
	var url = "/loginService";
	var data = { func : "updateCounter",
		currentCounter : $("#counterlist").val()
	}
	simpleservice(url,data,updateCounterSuccsess);
}

function updateCounterSuccsess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	if(objArray["pageType"] == "F"){
		window.location.href = "/task.jsp"; 
	}else if(objArray["pageType"] == "A"){
		window.location.href = "/adminconsole/"; 
	}else if(objArray["pageType"] == "H"){
		window.location.href = "/HouseKeeping/"; 
	}else if(objArray["pageType"] == "P"){
		window.location.href = "/POS/index.jsp?posId="+objArray["workArea"]["id"]+"&posTitle="+objArray["workArea"]["Title"]; 
	}
}

function loadWorkArea(){
	waitingDialog({});
	var url = "/loginService";
	var data = { func : "loadWorkArea" }
	simpleservice(url,data,fillWorkArea);
}

function fillWorkArea(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	$("#workarea").css("display","");
	$("#counter").css("display","none");
	var WA = objArray["data"];
	var sumtext = "<tr>";
	for(var i=0;i<WA.length;i++){
		if(WA[i]["Title"] == "iFront"){
			sumtext += "<td name=\"icon_front\"><div class=\"icon_front link\" onclick=\"saveSession('F',"+WA[i]["id"]+")\"><div class=\"img_front\"></div>Frontdesk</div></td>";
			break;
		}
	}
	for(var i=0;i<WA.length;i++){
		if(WA[i]["Title"] != "iFront"){
			sumtext += "<td name=\"icon_pos\"><div class=\"icon_pos link\" onclick=\"saveSession('P',"+WA[i]["id"]+")\"><div class=\"img_pos\"></div>"+WA[i]["Title"]+"</div></td>";
		}
	}
	if(objArray["adminAllowed"]){
		sumtext += "<td><div class=\"icon_admin link\" onclick=\"saveSession('A',null)\"><div class=\"img_admin\"></div>Admin Console</div></td>";
	}
	if(objArray["housekeepingAllowed"]){
		sumtext += "<td><div class=\"icon_housekeeping link\" onclick=\"saveSession('H',null)\"><div class=\"img_housekeeping\" ></div>House Keeping</div></td>";
	}
	sumtext += "</tr>";
	$("#arealist").children().remove();
	$("#arealist").append(sumtext);

	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function loadDataAndWorkArea(){
	var url = "/loginService";
	var data = { func : "loadDataAndWorkArea" }
	simpleservice(url,data,fillDataAndWorkArea);
}

function fillDataAndWorkArea(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	fillUserData(objArray["user"]);
	$("#workarea").css("display","");
	$("#counter").css("display","none");
	var WA = objArray["data"];
	var sumtext = "<tr>";
	for(var i=0;i<WA.length;i++){
		if(WA[i]["Title"] == "iFront"){
			sumtext += "<td name=\"icon_front\"><div class=\"icon_front link\" onclick=\"saveSession('F',"+WA[i]["id"]+")\"><div class=\"img_front\"></div>Frontdesk</div></td>";
			break;
		}
	}
	for(var i=0;i<WA.length;i++){
		if(WA[i]["Title"] != "iFront"){
			sumtext += "<td name=\"icon_pos\"><div class=\"icon_pos link\" onclick=\"saveSession('P',"+WA[i]["id"]+")\"><div class=\"img_pos\"></div>"+WA[i]["Title"]+"</div></td>";
		}
	}
	if(objArray["adminAllowed"]){
		sumtext += "<td><div class=\"icon_admin link\" onclick=\"saveSession('A',null)\"><div class=\"img_admin\"></div>Admin Console</div></td>";
	}
	if(objArray["housekeepingAllowed"]){
		sumtext += "<td><div class=\"icon_housekeeping link\" onclick=\"saveSession('H',null)\"><div class=\"img_housekeeping\" ></div>House Keeping</div></td>";
	}
	sumtext += "</tr>";
	$("#arealist").append(sumtext);
}

function loadDataAndCounter(){
	var url = "/loginService";
	var data = { func : "loadDataAndCounter" }
	simpleservice(url,data,fillDataAndCounter);
}

function fillDataAndCounter(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	fillUserData(objArray["user"]);
	fillCounter(data);
}

function fillUserData(objArray){
	$("[name=fullname]").text(objArray["name"]);
	$("#userlevel").text(objArray["userLevel"]);
	$("#hotelname").text(objArray["hotelName"]);
	$("#email").text(objArray["email"]);
	$("#lastNightAudit").text(new Date(objArray["lastNightAudit"]).getFormatedString());
}

function saveSession(pType,id){
	waitingDialog({});
	var url = "/loginService";
	var data = { 
			func : "saveSessionAndLoadCounter",
			pType : pType,
			id : id
			}
	simpleservice(url,data,fillCounter);
}


function fillCounter(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	$("#workarea").css("display","none");
	$("#counter").css("display","");
	$("#counterlist").children().remove();
	for(var i=0;i<objArray['data'].length;i++){
		var cStatus = "Close";
		var tStatus = "F";
		var status = objArray['data'][i]['isOpen'];
		if(status){
			cStatus = "Open";
			tStatus = "T";
		}
		$("#counterlist").append($("<option>").val(objArray['data'][i]['id']).html(objArray['data'][i]['title']+" ("+cStatus+")").attr("status",tStatus));
	}
	$("#counterlist").val(objArray['defaultCounter']);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deleteSession(){
	waitingDialog({});
	
	var url = "/loginService";
	var data = { 
			func : "deleteSession"
			}
	simpleservice(url,data,fillWorkArea);	
}

function next(){
	waitingDialog({});
	if($("#counterlist option:selected").attr("status") == "T"){
		upDateCounter();
	}else{
		var buttons = [
			{ title:"Cancel", callback: function(thisdialog,args){
				thisdialog.dialog("close");
				if($("#loadingScreen").is(":visible")) closeWaitingDialog();
			}},
			{ title:"Continue", callback: function(thisdialog,args){
				thisdialog.dialog("close");
				OpenCounter();
			}}
		];
		//var size = { width: 800, height: 200};
		var content = "This Counter Is Closed , Do You want to Continue?";
		customDialog("Closed Counter",content,null,buttons);
	}
}

function OpenCounter(){
	waitingDialog({});
	var url = "/counterpaymentservice";
	var data = { 
			func : "getLastPaymentData",
			counter : $("#counterlist :selected").val()
			}
	simpleservice(url,data,fillOpenCounterData);
}

function fillOpenCounterData(data){
	CreateOpenCounterDialog();
	var objArray1 = new Array();
	objArray1 = JSON.parse(data);
	var objArray = objArray1["data"];
	var symbol = objArray1["symbol"];
	var sumtext = "";
	for(var i=0;i<objArray.length;i++){
		sumtext += '<tr><td>&nbsp;</td><td class="tr-content">&nbsp;</td>'+
		'<td class="tr-content" name="id">'+(i+1)+'.</td>'+
		'<td class="tr-content" name="funds">'+objArray[i]['title']+'</td>'+
		'<td class="tr-content" name="lastAmount"><span name="symbol"></span> '+objArray[i]['amount']+'</td>'+
		'<td class="tr-content">&nbsp;</td><td>&nbsp;</td></tr>';
	}
	$("#tablehead").after(sumtext);
	$("[name=symbol]").html(symbol);
}

function CreateOpenCounterDialog(){
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
	var content = '<div id="OpenCounter" style="width:780px"><div><div style="font-size:25px;font-weight:bolder;text-align:center"><span>Open New Cash Counter : </span><span style="color:#0B65EA"></span></div></div><div style="margin-top: 15px;"><table id="tablelist" style="width:100%;border-collapse:collapse;"><tr id="tablehead" style="font-weight:bold;background-color:#4B68A0;color:#FFFFFF"><td class="table-head" style="width:3%">&nbsp;</td><td class="table-head" style="width:3%">&nbsp;</td><td class="table-head" style="width:5%">#</td><td class="table-head" >Funds</td><td class="table-head" style="width:15%">Last Closing Amount</td><td class="table-head" style="width:3%">&nbsp;</td><td class="table-head" style="width:3%">&nbsp;</td></tr><td colspan="7" class="last-tr" style="text-align:center" >Open Amount : <input id="amount" style="text-align: right;" type="text" value="0.00"/></td></table></div></div>';
	var buttons = [
	   			{ title:"Enter", callback: function(thisdialog,args){
	   				OpenedCounter();
	   				thisdialog.dialog("close");
	   			}},
	   			{ title:"Back To Select Counter", callback: function(thisdialog,args){
	   				thisdialog.dialog("close");
	   				if($("#loadingScreen").is(":visible")) closeWaitingDialog();
	   			}}
	   		];
	   		var size = { width: 800};
	   		var dialog = customDialog("",content,size,buttons);
	   		$("button.btn").css("float","");
}

function OpenedCounter(){
	waitingDialog({});
	var url = "/counterpaymentservice";
	var data = { func : "CreateCounterPayment",
		currentCounter : $("#counterlist :selected").val(),
		amount : $("#amount").val()
	}
	simpleservice(url,data,updateCounterSuccsess);
}